package gwt.trading.client;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import gwt.trading.client.util.Closure;
import gwt.trading.client.util.GwtList;

/**
 * An axis coordinate object.
 * 
 * @author Iulian Costan (iulian.costan@gmail.com)
 * 
 */
public class AxisCoordinate {

	private Axis verticalAxis;
	private Axis horizontalAxis;
	private DrawingManager drawingManager;

	private Map /* <String, Attachable> */attachables = new HashMap();

	public AxisCoordinate(Axis verticalAxis, DrawingManager drawingManager) {
		this(verticalAxis, null, drawingManager);
	}

	public AxisCoordinate(Axis verticalAxis, Axis horizontalAxis, DrawingManager drawingManager) {
		this.verticalAxis = verticalAxis;
		this.horizontalAxis = horizontalAxis;
		this.drawingManager = drawingManager;
	}

	public void attach(Attachable attachable) {
		attachable.setDrawingManager(drawingManager);
		attachables.put(attachable.getId(), attachable);
	}

	public void draw(final Renderer renderer) {
		verticalAxis.draw(renderer);

		if (horizontalAxis != null) {
			horizontalAxis.draw(renderer);
		}

		Collection list = attachables.values();
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			Drawable drawable = (Drawable) iterator.next();
			drawable.draw(renderer);
		}
	}

	public Attachable getAttachable(String string) {
		Collection list = attachables.values();
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			Attachable attachable = (Attachable) iterator.next();
			if (string.equalsIgnoreCase(attachable.getId())) {
				return attachable;
			}
		}
		return null;
	}

	public Attachable remove(String id) {
		return (Attachable) attachables.remove(id);
	}

}
